package com.ewzj.bigdata.mvp.ui.activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.ewzj.bigdata.R;
import com.ewzj.bigdata.app.MyGlideEngine;
import com.ewzj.bigdata.app.utils.UriToPathUtil;
import com.ewzj.bigdata.di.component.DaggerFeedBackComponent;
import com.ewzj.bigdata.di.module.FeedBackModule;
import com.ewzj.bigdata.mvp.contract.FeedBackContract;
import com.ewzj.bigdata.mvp.presenter.FeedBackPresenter;
import com.ewzj.bigdata.mvp.ui.adapter.PicItemAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class FeedBackActivity extends BaseActivity<FeedBackPresenter> implements FeedBackContract.View {


    @BindView(R.id.tv_confirm)
    TextView tv_confirm;
    @BindView(R.id.et_suggess)
    EditText etSuggess;
    @BindView(R.id.rv_pic)
    RecyclerView rvPic;
    private PicItemAdapter picItemAdapter;
    private List<Uri> paths;
    final int REQUEST_CODE_CHOOSE=16787;
    List<Uri> mSelected;
    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerFeedBackComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .feedBackModule(new FeedBackModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_feedback; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }
    private void initAdapter(){
        paths=new ArrayList<>();
        picItemAdapter=new PicItemAdapter(paths);
        View addview= LayoutInflater.from(this).inflate(R.layout.view_add,null,false);
        addview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPicSelected();
            }
        });
        picItemAdapter.addFooterView(addview);
        picItemAdapter.setFooterViewAsFlow(true);
    }
    @Override
    public void initData(Bundle savedInstanceState) {
        tv_confirm.setVisibility(View.VISIBLE);
        tv_confirm.setText("提交");
        initAdapter();
        rvPic.setLayoutManager(new GridLayoutManager(this,4));
        rvPic.setAdapter(picItemAdapter);
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @OnClick(R.id.tv_confirm)
    public void onViewClicked() {
        String suggess=etSuggess.getText().toString();
        if (TextUtils.isEmpty(suggess)){
            ArmsUtils.snackbarText("反馈内容不能为空");
            return;
        }
        mPresenter.FeedBack(suggess,mSelected);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_CHOOSE && resultCode == RESULT_OK) {
            mSelected = Matisse.obtainResult(data);
            picItemAdapter.addData(mSelected);
            Log.d("Matisse", "mSelected: " + mSelected);
        }
    }
    private void showPicSelected(){
        Matisse.from(this)
                .choose(MimeType.allOf())
                .countable(true)
                .maxSelectable(8)
                .gridExpectedSize(getResources().getDimensionPixelSize(R.dimen.grid_expected_size))
                .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_USER)
                .thumbnailScale(0.85f)
                .imageEngine(new MyGlideEngine())
                .forResult(REQUEST_CODE_CHOOSE);
    }

    @Override
    public void feedSuccess() {
        ArmsUtils.snackbarText("我们已经收到你的反馈");
        killMyself();
    }
}
